<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * 课程激活码组
 */
class NetCourseCdkeyGroup extends BaseModel
{
    protected $guarded = [];

    protected $appends = ['usedCount'];

    public function learnVideo(): BelongsTo
    {
        return $this->belongsTo(LearnVideo::class, 'dataId');
    }

    public function courseColumn(): BelongsTo
    {
        return $this->belongsTo(CourseColumn::class, 'dataId');
    }

    public function getUsedCountAttribute(): int
    {
        return NetCourseCdkey::where('groupId', $this->id)->whereIn('status', [2, 4])->count();
    }
}
